package cn.signalpha.modules.system.controller;

import cn.signalpha.common.cache.UserCache;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.cache.utils.CacheUtil;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.secure.annotation.PreAuth;
import cn.signalpha.core.secure.constant.AuthConstant;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.constant.RoleConstant;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.system.entity.Role;
import cn.signalpha.modules.system.entity.User;
import cn.signalpha.modules.system.service.IRoleService;
import cn.signalpha.modules.system.vo.GrantVO;
import cn.signalpha.modules.system.vo.RoleVO;
import cn.signalpha.modules.system.wrapper.RoleWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static cn.signalpha.core.cache.constant.CacheConstant.SYS_CACHE;

/**
 * 控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/admin/role")
@Api(value = "角色", tags = "角色")
@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
public class RoleController extends BaseController {

	private final IRoleService roleService;

	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "roleName", value = "参数名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "roleAlias", value = "角色别名", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "列表", notes = "传入role")
	public R<List<RoleVO>> list(@ApiIgnore @RequestParam Map<String, Object> role) {
		QueryWrapper<Role> queryWrapper = Condition.getQueryWrapper(role, Role.class);
		List<Role> list = roleService.list((queryWrapper));
		return R.data(RoleWrapper.build().listNodeVO(list));
	}

	@GetMapping("/tree")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<RoleVO>> tree() {
		return R.data(roleService.tree());
	}

	@PreAuth(AuthConstant.PERMIT_ALL)
	@GetMapping("/select")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "下拉数据源", notes = "传入id集合")
	public R<List<Role>> select(Long userId, String roleId) {
		if (Func.isNotEmpty(userId)) {
			User user = UserCache.getUser(userId);
			roleId = user.getRoleId();
		}
		List<Role> list = roleService.list(Wrappers.<Role>lambdaQuery().in(Role::getId, Func.toLongList(roleId)));
		return R.data(list);
	}

	@GetMapping("/detail")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "详情", notes = "传入role")
	public R<RoleVO> detail(Role role) {
		Role detail = roleService.getOne(Condition.getQueryWrapper(role));
		return R.data(RoleWrapper.build().entityVO(detail));
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "新增或修改", notes = "传入role")
	public R submit(@Valid @RequestBody Role role) {
		CacheUtil.clear(SYS_CACHE);
		CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
		return R.status(roleService.submit(role));
	}

	@PostMapping("/grant")
	@ApiOperationSupport(order = 21)
	@ApiOperation(value = "权限设置", notes = "传入roleId集合以及menuId集合")
	public R grant(@RequestBody GrantVO grantVO) {
		CacheUtil.clear(SYS_CACHE);
		CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
		return R.status(roleService.grant(grantVO.getRoleIds(), grantVO.getMenuIds()));
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 50)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		CacheUtil.clear(SYS_CACHE);
		CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
		return R.status(roleService.removeRole(ids));
	}

}
